package com.calendar.month;

import java.util.Calendar;

import com.calendar.widget.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Button;
import android.widget.TextView;

public class CalendarView extends Activity implements Button.OnClickListener {


        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
        private GestureDetector gestureDetector;
        View.OnTouchListener gestureListener;


        Button one;
        Button two;
        Button three;
        Button four;
        Button five;
        Button six;
        Button seven;
        Button eight;
        Button nine;
        Button ten;
        Button eleven;
        Button twelve;
        Button thirteen;
        Button fourteen;
        Button fifteen;
        Button sixteen;
        Button seventeen;
        Button eighteen;
        Button nineteen;
        Button twenty;
        Button twentyone;
        Button twentytwo;
        Button twentythree;
        Button twentyfour;
        Button twentyfive;
        Button twentysix;
        Button twentyseven;
        Button twentyeight;
        Button twentynine;
        Button thirty;
        Button thirtyone; 
        Button thirtytwo;
        Button thirtythree;
        Button thirtyfour; 
        Button thirtyfive;
        Button thirtysix;
        Button thirtyseven;
        Button thirtyeight;
        Button thirtynine;
        Button fourty;
        Button fourtyone;
        Button fourtytwo;

        TextView monthyear;

        Calendar now;

        int month;
        int year;
        int day;
        int[] monthTable = new int[12];
        String[] months = new String[12];
        boolean leapYear;
        Button[][] calendar = new Button[6][7];

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.month_view);

                one = (Button) findViewById(R.id.one);
                two = (Button) findViewById(R.id.two);
                three = (Button) findViewById(R.id.three);
                four = (Button) findViewById(R.id.four);
                five = (Button) findViewById(R.id.five);
                six = (Button) findViewById(R.id.six);
                seven = (Button) findViewById(R.id.seven);
                eight = (Button) findViewById(R.id.eight);
                nine = (Button) findViewById(R.id.nine);
                ten = (Button) findViewById(R.id.ten);
                eleven = (Button) findViewById(R.id.eleven);
                twelve = (Button) findViewById(R.id.twelve);
                thirteen = (Button) findViewById(R.id.thirteen);
                fourteen = (Button) findViewById(R.id.fourteen);
                fifteen= (Button) findViewById(R.id.fifteen);
                sixteen= (Button) findViewById(R.id.sixteen);
                seventeen = (Button) findViewById(R.id.seventeen);
                eighteen = (Button) findViewById(R.id.eighteen);
                nineteen = (Button) findViewById(R.id.nineteen);
                twenty = (Button) findViewById(R.id.twenty);
                twentyone = (Button) findViewById(R.id.twentyone);
                twentytwo = (Button) findViewById(R.id.twentytwo);
                twentythree = (Button) findViewById(R.id.twentythree);
                twentyfour = (Button) findViewById(R.id.twentyfour);
                twentyfive = (Button) findViewById(R.id.twentyfive);
                twentysix = (Button) findViewById(R.id.twentysix);
                twentyseven = (Button) findViewById(R.id.twentyseven);
                twentyeight = (Button) findViewById(R.id.twentyeight);
                twentynine = (Button) findViewById(R.id.twentynine);
                thirty = (Button) findViewById(R.id.thirty);
                thirtyone = (Button) findViewById(R.id.thirtyone);
                thirtytwo = (Button) findViewById(R.id.thirtytwo);
                thirtythree = (Button) findViewById(R.id.thirtythree);
                thirtyfour = (Button) findViewById(R.id.thirtyfour);
                thirtyfive = (Button) findViewById(R.id.thirtyfive);
                thirtysix = (Button) findViewById(R.id.thirtysix);
                thirtyseven = (Button) findViewById(R.id.thirtyseven);
                thirtyeight = (Button) findViewById(R.id.thirtyeight);
                thirtynine = (Button) findViewById(R.id.thirtynine);
                fourty = (Button) findViewById(R.id.fourty);
                fourtyone = (Button) findViewById(R.id.fourtyone);
                fourtytwo = (Button) findViewById(R.id.fourtytwo);

                calendar[0][0] = one;
                calendar[0][1] = two;
                calendar[0][2] = three;
                calendar[0][3] = four;
                calendar[0][4] = five;
                calendar[0][5] = six;
                calendar[0][6] = seven;
                calendar[1][0] = eight;
                calendar[1][1] = nine;
                calendar[1][2] = ten;
                calendar[1][3] = eleven;
                calendar[1][4] = twelve;
                calendar[1][5] = thirteen;
                calendar[1][6] = fourteen;
                calendar[2][0] = fifteen;
                calendar[2][1] = sixteen;
                calendar[2][2] = seventeen;
                calendar[2][3] = eighteen;
                calendar[2][4] = nineteen;
                calendar[2][5] = twenty;
                calendar[2][6] = twentyone;
                calendar[3][0] = twentytwo;
                calendar[3][1] = twentythree;
                calendar[3][2] = twentyfour;
                calendar[3][3] = twentyfive;
                calendar[3][4] = twentysix;
                calendar[3][5] = twentyseven;
                calendar[3][6] = twentyeight;
                calendar[4][0] = twentynine;
                calendar[4][1] = thirty;
                calendar[4][2] = thirtyone;
                calendar[4][3] = thirtytwo;
                calendar[4][4] = thirtythree;
                calendar[4][5] = thirtyfour;
                calendar[4][6] = thirtyfive;
                calendar[5][0] = thirtysix;
                calendar[5][1] = thirtyseven;
                calendar[5][2] = thirtyeight;
                calendar[5][3] = thirtynine;
                calendar[5][4] = fourty;
                calendar[5][5] = fourtyone;
                calendar[5][6] = fourtytwo;


                monthyear = (TextView) findViewById(R.id.monthyear);

                months[0] = "January";
                months[1] = "February";
                months[2] = "March";
                months[3] = "April";
                months[4] = "May";
                months[5] = "June";
                months[6] = "July";
                months[7] = "August";
                months[8] = "September";
                months[9] = "October";
                months[10] = "November";
                months[11] = "December";

                monthTable[0] = 0;
                monthTable[1] = 3;
                monthTable[2] = 3;
                monthTable[3] = 6;
                monthTable[4] = 1;
                monthTable[5] = 4;
                monthTable[6] = 6;
                monthTable[7] = 2;
                monthTable[8] = 5;
                monthTable[9] = 0;
                monthTable[10] = 3;
                monthTable[11] = 5;

                now = Calendar.getInstance();   

                month = now.get(Calendar.MONTH);
                year = now.get(Calendar.YEAR);
                day = now.get(Calendar.DAY_OF_MONTH);

                //Set up my gesture detector just for swiping between months
                gestureDetector = new GestureDetector(new MyGestureDetector());
                gestureListener = new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {
                                if (gestureDetector.onTouchEvent(event)) {
                                        return true;
                                }
                                return false;
                        }
                };
                
                //Render current month
                renderMonth();
        }
        
        public void onClick(View view){
                //When a day is clicked start day view 
        }

        //Renders the month everytime you swipe one way or another
        public void renderMonth(){

                //Figures out what day the month starts on
                int lastDigit = year%2000;
                monthyear.setText(months[month] + " " + year);
                if(lastDigit%4 == 0){
                        leapYear = true;
                }

                int century = 6;
                int monthVal = monthTable[month];
                int total = century + monthVal + lastDigit + (lastDigit%4);
                int dayFinal = total % 7;
                int dayNum = 1;
                int monthLength = decideMonthTotal(leapYear);
                //Clears the old data from calendar
                clearCalendar();
                boolean breaker = false;
                //Writes new numbers to calendar
                for(int i = 0; i < 6; i ++){
                        if (breaker){
                                break;
                        }
                        for(int j = dayFinal; j < 7; j ++){
                                calendar[i][j].setTextSize(6);
                                calendar[i][j].setText("" + dayNum +"");
                                dayNum++;
                                if(monthLength < dayNum){
                                        dayFinal = 0;
                                        breaker = true;
                                        break;
                                }
                        }
                        dayFinal = 0;
                }
        }

        //Erases calendar
        public void clearCalendar(){
                for(int i = 0; i < 6; i ++){
                        for(int j = 0; j < 7; j ++){
                                calendar[i][j].setText("  ");
                        }
                }
        }

        //Decide the lenght of the month
        public int decideMonthTotal(boolean leap){
                if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7
                                || month == 9 || month == 11){
                        return 31;
                }
                else if(month == 1 && leap == true){
                        return 29;
                }
                else if(month == 1 && leap == false){
                        return 28;
                }
                else{
                        return 30;
                }
        }

        //Determines if the swipe was valid
        class MyGestureDetector extends SimpleOnGestureListener {
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        try {
                                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                                        return false;
                                // right to left swipe
                                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                                        month++;
                                        if(month == 12){
                                                year++;
                                                month = 0;
                                        }
                                        renderMonth();
                                }  
                                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                                        month--;
                                        if(month == -1){
                                                year--;
                                                month = 11;
                                        }
                                        renderMonth();
                                }
                        } catch (Exception e) {
                                // nothing
                        }
                        return false;
                }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                if (gestureDetector.onTouchEvent(event))
                        return true;
                else
                        return false;
        }
}